package com.goddess.yeecolor.fragment.ExerciseitemFragment;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.goddess.yeecolor.Modle.activity;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.ExerciseMapActivity;
import com.goddess.yeecolor.adapter.activityDetailsAdapter;
import com.goddess.yeecolor.base.BaseFragment;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.Constans;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.view.LoadingDialog;
import com.goddess.yeecolor.view.RecycleViewDivider;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动须知
 */
public class ExerciseItem_activityDetails extends BaseFragment implements View.OnClickListener{
    private List<activity>mList;
    private activityDetailsAdapter adapter;
    private TextView fragment_details_content;
    private View mView;
    private RecyclerView fragment_details_recyclerview;
    private LinearLayoutManager manager;
    private String array;
    private String phone;
    private Bundle mBundle;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView==null){
            mView=inflater.inflate(R.layout.fragment_activitydetails,container,false);
            initFindView();
            initOnClck();
        }
        return mView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle=getArguments();
//        LogUtils.getInstence().info(getArguments().toString());
        if (mBundle!=null){
            array=mBundle.getString("array");
        }
    }
    public ExerciseItem_activityDetails(){}
    public static ExerciseItem_activityDetails newInstance(String array) {
        ExerciseItem_activityDetails  newFragments= new ExerciseItem_activityDetails();
        Bundle bundle = new Bundle();
        bundle.putString("array",array);
        newFragments.setArguments(bundle);
        return newFragments;


    }
    private String ln,la,titles;
    @Override
    public void initOnClck() {
        adapter.setOnClickListener(new OnClickListener() {
            @Override
            public void OnClick(View view, int position) {
                activity activity=mList.get(position);
                if (activity.getFunc()!=null){
                    if (activity.getFunc().equals("map")){//地图
//地图
                        if (!ln.isEmpty()&&!la.isEmpty()){
                            Bundle bundle=new Bundle();
                            bundle.putString("ln",ln);
                            bundle.putString("la",la);
                            bundle.putString("title",titles);
                            JumpIntents.startIntent(getActivity(),ExerciseMapActivity.class,bundle);
                        }

                    }
                    else if (activity.getFunc().equals("tel")){
                        phone=activity.getTel();
                        if (!phone.isEmpty()){
                            if(hasPermissin(Manifest.permission.CALL_PHONE)){
                                doCall();
                            }
                            else {
                                //请求权限
                                requestPermission(Constans.WRIGTE_CALL_EXTERNAL_CODE,Manifest.permission.CALL_PHONE);
                            }
                        }

                    }
                }
                else {}
            }

            @Override
            public void OnLongClick(View view, int position) {

            }
        });
    }
    @Override
    public  void doCall(){

        LoadingDialog dialog=new LoadingDialog(getActivity(),getActivity(),phone,1);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
    @Override
    public void initFindView() {
        fragment_details_recyclerview= (RecyclerView) mView.findViewById(R.id.fragment_details_recyclerview);
        manager=new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        fragment_details_recyclerview.setLayoutManager(manager);
        fragment_details_recyclerview.addItemDecoration(new RecycleViewDivider(getActivity(), manager.HORIZONTAL, 1, Color.rgb(211, 211, 211)));
        getData(array);

    }

    @Override
    public void initData() {

    }

    /**
     * 获取数据
     */
    private void getData(String array){
        mList=new ArrayList<>();
        try {

            JSONArray jsonArray=new JSONArray(array);
            for (int i=0;i<jsonArray.length();i++){
                JSONObject data=jsonArray.getJSONObject(i);
                activity activity=new activity();
                activity.setText(data.optString("text"));
                activity.setType(data.optString("type"));
                if (data.has("image")){
                    activity.setImage(data.optString("image"));
                }
                if (data.has("func")){

                    activity.setFunc(data.optString("func"));
                }
                if (data.has("la")){
                    la=data.getString("la");
                    titles=data.optString("text");
                    activity.setLa(data.optString("la"));
                }
                if (data.has("ln")){
                    ln=data.optString("ln");
                    activity.setLn(data.optString("ln"));
                }
                if (data.has("tel")){
                    activity.setTel(data.optString("tel"));
                }
                mList.add(activity);
            }
            adapter=new activityDetailsAdapter(mList,getActivity(),0);
            fragment_details_recyclerview.setAdapter(adapter);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){

        }
    }
}
